/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ViewGrupo.java
 *
 * Created on 19/03/2012, 11:13:41
 */

package com.qp.smsmarketing.view;

import com.qp.smsmarketing.utils.JTextFieldFilter;
import com.qp.smsmarketing.view.mbean.GrupoMBean;
import javax.swing.JOptionPane;

/**
 *
 * @author waldenilson
 */
public class ViewGrupo extends AbstractDialog {

    GrupoMBean gmbean = new GrupoMBean();

    /** Creates new form ViewGrupo */
    public ViewGrupo() {
        initComponents();
        setTitle(".:: Grupos : SMS-Marketing ::.");
        setLocationRelativeTo(null);
//        tf_nome.setDocument(new JTextFieldFilter(JTextFieldFilter.ALPHA_NUMERIC,50));
//        tf_desc.setDocument(new JTextFieldFilter(JTextFieldFilter.ALPHA_NUMERIC,160));
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        bt_salvar = new javax.swing.JButton();
        tf_nome = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tableGrupo = new javax.swing.JTable();
        jScrollPane2 = new javax.swing.JScrollPane();
        tf_desc = new javax.swing.JTextArea();
        bt_novo = new javax.swing.JButton();
        cb_ativo = new javax.swing.JCheckBox();
        jButton1 = new javax.swing.JButton();

        setBackground(new java.awt.Color(255, 255, 255));
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("GRUPOS");

        bt_salvar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/qp/smsmarketing/resource/icon/16X16/apply.png"))); // NOI18N
        bt_salvar.setText("Salvar");
        bt_salvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_salvarActionPerformed(evt);
            }
        });

        jLabel3.setText("Descrição.:");

        jLabel2.setText("Nome.:");

        tableGrupo.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Código", "Nome", "Descrição", "Ativo ?"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Boolean.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tableGrupo.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tableGrupoMouseClicked(evt);
            }
        });
        tableGrupo.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tableGrupoKeyPressed(evt);
            }
        });
        jScrollPane1.setViewportView(tableGrupo);

        tf_desc.setColumns(20);
        tf_desc.setRows(5);
        jScrollPane2.setViewportView(tf_desc);

        bt_novo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/qp/smsmarketing/resource/icon/16X16/add.png"))); // NOI18N
        bt_novo.setText("Novo");
        bt_novo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_novoActionPerformed(evt);
            }
        });

        cb_ativo.setText("Grupo Ativo ?");
        cb_ativo.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        cb_ativo.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/qp/smsmarketing/resource/icon/16X16/cancel.png"))); // NOI18N
        jButton1.setText("Sair");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(tf_nome, javax.swing.GroupLayout.PREFERRED_SIZE, 154, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 388, Short.MAX_VALUE)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(cb_ativo)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 28, Short.MAX_VALUE)
                        .addComponent(bt_novo)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bt_salvar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 368, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(tf_nome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cb_ativo)
                    .addComponent(jButton1)
                    .addComponent(bt_novo)
                    .addComponent(bt_salvar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tableGrupoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tableGrupoMouseClicked

        gmbean.selecionarRegistro(this);
        
        // TODO add your handling code here:
    }//GEN-LAST:event_tableGrupoMouseClicked

    private void bt_novoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_novoActionPerformed

        gmbean.novoObjeto(this);
        // TODO add your handling code here:
    }//GEN-LAST:event_bt_novoActionPerformed

    private void bt_salvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_salvarActionPerformed
        String retorno = gmbean.salvarObjeto(this);
        if ( retorno.equals("error") )
            JOptionPane.showMessageDialog
                    (null,"Ocorreu um erro em salvar modificações.\nTente novamente.","Erro",2);
        else if(retorno.equals("verify"))
            JOptionPane.showMessageDialog
                    (null,"Informe dados corretos.\nTente novamente.","Erro",2);
        else
            gmbean.buscarCrudGrupo(this);
        // TODO add your handling code here:
    }//GEN-LAST:event_bt_salvarActionPerformed

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed

        
        // TODO add your handling code here:
    }//GEN-LAST:event_formWindowClosed

    private void tableGrupoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tableGrupoKeyPressed
            if(evt.getKeyCode() == 127)
            {
                String retorno = gmbean.removerObjeto(this);
                if ( retorno.equals("error") )
                JOptionPane.showMessageDialog
                        (null,"Ocorreu um erro em remover o registro.\nTente novamente.","Erro",2);
                else
                    gmbean.buscarCrudGrupo(this);
            }
    }//GEN-LAST:event_tableGrupoKeyPressed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

        dispose();
        // TODO add your handling code here:
}//GEN-LAST:event_jButton1ActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ViewGrupo().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bt_novo;
    private javax.swing.JButton bt_salvar;
    private javax.swing.JCheckBox cb_ativo;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable tableGrupo;
    private javax.swing.JTextArea tf_desc;
    private javax.swing.JTextField tf_nome;
    // End of variables declaration//GEN-END:variables

    /**
     * @return the tableGrupo
     */
    public javax.swing.JTable getTableGrupo() {
        return tableGrupo;
    }

    /**
     * @param tableGrupo the tableGrupo to set
     */
    public void setTableGrupo(javax.swing.JTable tableGrupo) {
        this.tableGrupo = tableGrupo;
    }

    /**
     * @return the tf_desc
     */
    public javax.swing.JTextArea getTf_desc() {
        return tf_desc;
    }

    /**
     * @param tf_desc the tf_desc to set
     */
    public void setTf_desc(javax.swing.JTextArea tf_desc) {
        this.tf_desc = tf_desc;
    }

    /**
     * @return the tf_nome
     */
    public javax.swing.JTextField getTf_nome() {
        return tf_nome;
    }

    /**
     * @param tf_nome the tf_nome to set
     */
    public void setTf_nome(javax.swing.JTextField tf_nome) {
        this.tf_nome = tf_nome;
    }

    /**
     * @return the cb_ativo
     */
    public javax.swing.JCheckBox getCb_ativo() {
        return cb_ativo;
    }

    /**
     * @param cb_ativo the cb_ativo to set
     */
    public void setCb_ativo(javax.swing.JCheckBox cb_ativo) {
        this.cb_ativo = cb_ativo;
    }

    /**
     * @return the bt_novo
     */
    public javax.swing.JButton getBt_novo() {
        return bt_novo;
    }

    /**
     * @param bt_novo the bt_novo to set
     */
    public void setBt_novo(javax.swing.JButton bt_novo) {
        this.bt_novo = bt_novo;
    }

    /**
     * @return the bt_salvar
     */
    public javax.swing.JButton getBt_salvar() {
        return bt_salvar;
    }

    /**
     * @param bt_salvar the bt_salvar to set
     */
    public void setBt_salvar(javax.swing.JButton bt_salvar) {
        this.bt_salvar = bt_salvar;
    }

}
